package com.yyzw.activiti.modeler.controller;

import com.yyzw.activiti.modeler.model.DataGridRespVo;
import com.yyzw.activiti.modeler.model.ResponseVo;
import com.yyzw.activiti.modeler.model.SysUser;
import com.yyzw.activiti.modeler.model.TaskHandleVo;
import com.yyzw.activiti.modeler.service.MyTaskService;
import org.activiti.engine.TaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

@RestController
@RequestMapping(value = "task")
public class TaskController {

    @Autowired
    MyTaskService myTaskService;

    @RequestMapping("/showTaskList")
    public ModelAndView showTaskList(HttpServletRequest request,ModelAndView modelAndView) throws Exception{
        modelAndView.setViewName("task/taskList");
        return modelAndView;
    }

    @RequestMapping("/getTaskList")
    public DataGridRespVo getTaskList(HttpServletRequest request,String page,String rows)throws Exception{
        SysUser userSession = (SysUser)request.getSession().getAttribute("userSession");
       return  myTaskService.getUserTaskList(userSession,Integer.parseInt(page),Integer.parseInt(rows));
    }

    @RequestMapping("/showHanldeTaskById")
    public ResponseVo showHanldeTaskById(HttpServletRequest request)throws Exception{
        String taskId = request.getParameter("taskId");
        return myTaskService.showHanldeTaskById(taskId);
    }

    @RequestMapping("handleTask")
    public ResponseVo handleTask(TaskHandleVo taskHandleVo, HttpServletRequest request) throws Exception{
        SysUser sysUser =(SysUser)request.getSession().getAttribute("userSession");
        taskHandleVo.setHandleId(String.valueOf(sysUser.getUserId()));
        taskHandleVo.setHandleName(sysUser.getUserName());
        taskHandleVo.setCreateTime(new Date());
        return myTaskService.handleTask(taskHandleVo);
    }
}
